//
//  NoticeDetailViewController.m
//  51易借
//
//  Created by 张睿 on 2017/9/12.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "NoticeDetailViewController.h"
#import "MessageDetailTableViewCell.h"



@interface NoticeDetailViewController ()<UIWebViewDelegate>

@property (nonatomic,strong)UIWebView * web;
//@property (nonatomic,strong)NoticeListModel * messageDetail;
@property (nonatomic, weak)UIView * topView;

@end

@implementation NoticeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    [self setupUI];
//    [self getMessageDetail];
    [self loadWebview];
}


- (void)setupNavBar{
    
    self.title = self.titleString;
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}



- (void)setupUI{
    
    //    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.view.backgroundColor = BACK_COLOR;
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = ColorWithRGB(0xff7907);
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    

    self.automaticallyAdjustsScrollViewInsets = NO;
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 70 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT-70 * SCREENH6)];
    self.web.scalesPageToFit = YES;
    self.web.delegate = self;
    [self.view addSubview:self.web];
    
//    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero];
//    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    self.tableView.separatorStyle = NO;
//    self.tableView.estimatedRowHeight = 100;
//    [self.view addSubview:self.tableView];
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
//    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MessageDetailTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"item"];
    
}

- (void)loadWebview{
    
    NSDictionary * parameters = @{@"announceId":self.ID};
    
    [[NetworkTool shareManager] POST:@"content/announce/getAnnounceDetail.json" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        
        NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        
        if ([code isEqualToString:@"0"]) {
            
            NSDictionary * data = responseObject[@"data"];
            
            [self.web loadHTMLString:data[@"contentHtml"] baseURL:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"==%@",error);
        
    }];
}

- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
//    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.leading.trailing.mas_equalTo(self.view);
//        make.top.mas_equalTo(self.topView.mas_bottom);
//    }];
    
}
//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return self.messageDetail?1:0;
//}
//-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    MessageDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"item" forIndexPath:indexPath];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.notice = self.messageDetail;
//    return cell;
//}

- (void)backToLastView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)webViewDidFinishLoad:(UIWebView*)webView {
    
    
    [self.web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '200%'"];
    
}

-(void)getMessageDetail
{
//    ParaModel*para = [ParaModel new];
//    para.announceId = self.ID;
//    
//    [[NetworkTool shareManager] POST:[Interfacefile getAnnounceDetail] Param:[para toDictionary] andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
//        
////        NSLog(@"notice%@",responseObject);
//        if([[responseObject objectForKey:@"code"]intValue]==0)
//        {
//            self.messageDetail = [[NoticeListModel alloc] initWithDictionary:[responseObject objectForKey:@"data"] error:nil];
//            
//            [self.tableView reloadData];
//        }else{
//            
//            [self showHint:[responseObject objectForKey:@"message"]];
//            
//            
//        }
//        [self.tableView reloadData];
//    } andFailure:^(NSError * _Nonnull error) {
//        [self showHint:@"网络错误"];
//    }];
    
}


@end
